﻿using System;
using System.IO;
using Devv.Core.Utils;

namespace Devv.MonFiMailer
{
	[Serializable]
	public class Options
	{
		#region Properties

		private static String XmlPath = Program.BasePath + "Options.xml";

		public String FolderPath { get; set; }
		public String FolderFilter { get; set; }
		public Boolean IncludeSubDir { get; set; }

		public String SmtpServer { get; set; }
		public Int32 SmtpPort { get; set; }
		public Boolean SmtpSSL { get; set; }
		public String SmtpUser { get; set; }
		public String SmtpPassword { get; set; }

		public String EmailFrom { get; set; }
		public String EmailTo { get; set; }
		public String EmailSubject { get; set; }

		public Int32 PerMinute { get; set; }

		#endregion

		#region Load

		public static Options Load()
		{
			Options o = File.Exists(XmlPath) ? SerializationUtil.Deserialize(XmlPath, typeof(Options)) as Options : new Options();

			if (o == null)
			{
				o = new Options();
			}

			if (o.PerMinute < 1)
			{
				o.PerMinute = 10;
			}

			if (String.IsNullOrEmpty(o.FolderPath) && String.IsNullOrEmpty(o.SmtpServer) && String.IsNullOrEmpty(o.EmailFrom) && String.IsNullOrEmpty(o.EmailTo))
			{
				o.FolderFilter = "*";
				o.SmtpServer = "smtp.google.com";
				o.SmtpPort = 25;
			}

			return o;
		}

		#endregion

		#region Save

		public void Save()
		{
			String xml = SerializationUtil.Serialize(this);

			IOUtil.WriteFile(XmlPath, xml);
		}

		#endregion
	}
}